import java.awt.Color;

import java.awt.Graphics;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.*;

public class TinCDCanvas extends JPanel{
	
	String []alkaneNames = {"","Methane","Ethane","Propane","Butane","Pentane","Hexane","Heptane"};
	int NUM_OF_CARBONS = 4;
	Point cursorLocation;
	int x,y;
	Line2D.Double benzeneArr[][] = new Line2D.Double[100][9];
	Line2D.Double NULL = new Line2D.Double(0,0,0,0);
	boolean drawSavedItem = false;
	static int ROWCOUNT = 0;
	int panelHeight = 0;
	int panelWidth = 0;
	int newInstance = 0;
	private MainFrame TinCDCopy;
	
	private Color backgroundColor = new Color(0.0f,0.0f,0.0f);
	
	TinCDCanvas(MainFrame TinCD)
	{
		TinCDCopy = TinCD;
		addMouseMotionListener(new MouseMovingHandler());
		addMouseListener(new MouseHandler());
		Carbon carbon = new Carbon();
		Hydrogen hydrogen = new Hydrogen();
		AtomicBond atomicbond = new AtomicBond(carbon,hydrogen);
	
	}

	public void paintComponent(Graphics g)
	{
		
		
		super.paintComponent(g);
		
		
		panelHeight = TinCDCopy.getHeight();
		panelWidth = TinCDCopy.getWidth();
		
		//Draws a white box for drawing 400 x 400
		Graphics2D gg = (Graphics2D)g;
		g.setColor(backgroundColor);
		g.fillRect(0, 0, panelWidth, panelHeight);
		
		//Sets the skeletal form lines to black
		
		g.setColor(new Color(0,0,0));
		
		//For simple carbon alkanes, the bonds need to be ~109.5 degrees.
		//This calls for a slope of 1/2 at the moment to make it somewhat realistic
		//The carbon chain should actually start on a positive slope from the origin.
		//General formula should be "x,y,x+50,y-25" then, "x+50,y-25,x+50+50,y"
		//A good start is to have each bond length ~50 pixels in length
		//Remember, as a rule of thumb, the number of bonds drawn is #Carbons - 1 for straight chain
		//alkanes
		//Y values are alternating depending on the number of bonds
		//Could easily have ~10 values stored in a string array to draw the Text of which one
		//is being drawn.
		
		//Need to have a special case for methane.
		//This could parlay into a special feature where you could draw the carbons and the 
		//hydrogens in.
		
		//Think about having an easy to use drawing system. Point and click is a joke to do.
		//However, I will probably have to implement a correcting algorithm to make sure the bonds are
		//correct. Have to think about how to implement the text boxes on command though....
		
		
		//Also think about making an algorithm to make cycloalkanes. This is easy enough, just need
		//some simple geometry going.
		
		//Perhaps have a different panel type for each type of molecule? Like straight chain alkanes
		//and cycloalkanes?
		
		/*g.drawLine(25,75, 75, 50);
		g.drawLine(75,50,125,75);
		g.drawLine(125,75, 175, 50);
		
		g.drawString(alkaneNames[NUM_OF_CARBONS],100,100);
		*/
		
		//Testing some lewis structure stuff
		//It appears that the Character is ~10 pt font.
		
		//g.drawLine (196,200,175,200);	//left arm
		//g.drawLine (204,200,225,200);	//right arm
		//g.drawLine(200,194,200,175);	//upward arm
		//g.drawLine(200,204,200,225);	//downward arm
		
		
		//For the character 'C' to be positioned in the middle of the cross intersection
		//It needed to be placed four pixels to the left of the origin x and 4 pixels down
		//from the origin y
		//g.drawString("C",196,204);	//right in the intersection
		
		//Now for some hydrogen fun
		//For the character 'H' to be positioned next to the correct arm it needed to be placed
		// 10 pixels away from the end of the arm. (I'm assuming that the character is actually 10
		// x 10 pixels. Hence 10 pt font.
		//However it is looking for the coordinates of the left most starting area. So on the right arm
		//it doesn't need many buffer pixels whatsoever.
		
		//g.drawString("H",165,204);	//left arm hydrogen	10 pixels left,4 down 
		//g.drawString("H",227,204);		//right arm hydrogen 2 pixels right, 4 down
		//g.drawString("H",196,175);		//upward arm hydrogen 4 pixels left, 0 down
		//g.drawString("H",196,235);		//downward arm hydrogen 4 pixels left, 10 down
		
		//I need to figure out a method to generically draw all of these. Badly.
		//Probably need to preserve methane's method then try ethane to see how that goes. Then
		//I could derive a basic formula
		
		//Now lets try some ethane!
		
		//g.drawLine (196,200,175,200);	//left arm
		//g.drawLine (204,200,225,200);	//right arm
		//g.drawLine(200,194,200,175);	//upward arm
		//g.drawLine(200,204,200,225);	//downward arm
		
		//g.drawString("C",196,204);
		
		//g.drawString("Cl",165,204);	//left arm hydrogen	10 pixels left,4 down 
		//g.drawString("Cl",196,235);		//downward arm hydrogen 4 pixels left, 10 down
		//g.drawString("H",196,175);		//upward arm hydrogen 4 pixels left, 0 down
		
		//g.drawString("C", 227, 204);
		
		//g.drawLine(231,194,231,175); //upward arm
		//g.drawLine(231,204,231,225);	//downward arm
		//g.drawLine(237,200,262,200);// right arm
		
		//g.drawString("F", 227,175 ); //upward arm hydrogen
		//g.drawString("H", 227, 235); //downward arm hydrogen
		//g.drawString("Br",264,204);	//right arm hydrogen
		
		//As a rule of thumb here is how the spacing should go 
		// Left bond to hydrogen ---- 10 pixels left of end of arm, 4 pixels down from arm
		// Right bond to hydrogen ---- 2 pixels right of end of arm, 4 pixels down from arm
		// Upward bond to hydrogen ---- 4 pixels left, 0 pixels down from arm
		// Downward bond to hydrogen ---- 4 pixels left, 10 pixels down from arm
		
		//Now lets try and make some cycloalkanes!
		//Make sure to have some kind of check to make sure the users don't try to make
		//a molecule with less than 3 carbons
		
		//This part especially needs some thought behind it, actually implementing geometry
		
		//For now lets try and make the straight alkanes generic with a simple for loop
		//"x,y,x+50,y-25" then, "x+50,y-25,x+50+50,y"
		//alternating powers of a negative number will allow the oscillation
		
		/*
		 * THIS IS IMPORTANT! ANTIALIASING ALLOWS FOR VERY SMOOTH LINES
		 *
		 */
		/*for (int i = 0; i<2 ;i++ )
		{
			int x = 60;
			int y1;
			
			if (i%2 != 0)
			y1 = 35;
			else
			y1 = 60;
			
			gg.setPaint(Color.BLACK);
			gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
			gg.setStroke(new BasicStroke(1.4f, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));;
			
			gg.draw( new Line2D.Double(x+(25*i),y1,x+25+(25*i),y1-(int)(Math.pow(-1, i)*25)));
			System.out.printf("X1: %d, Y1: %d, X2: %d, Y2: %d\n",(x+(25*i)),y1,x+25+(25*i),y1-(int)(Math.pow(-1, i)*25));
			//.drawLine(x+(50*i),y1, x+50+(50*i),y1-(int)(Math.pow(-1, i)*25));
			
		
		}
		*/
		gg.setPaint(Color.WHITE);
		gg.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
		gg.setStroke(new BasicStroke(1.4f, BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND));;
		
		/*gg.draw(new Line2D.Double(50,50,90,50));
		gg.draw(new Line2D.Double(90,50,120,70));
		gg.draw(new Line2D.Double(120,70,120,110));
		gg.draw(new Line2D.Double(120,110,90,135));
		gg.draw(new Line2D.Double(90,135,50,135));
		gg.draw(new Line2D.Double(50,135,20,110));
		gg.draw(new Line2D.Double(20,110,20,70));
		gg.draw(new Line2D.Double(20,70,50,50));
		
		*/
	
	gg.draw( new Line2D.Double(x,y,x+35,y-20));	//x,y,x + 35, y - 10
		gg.draw( new Line2D.Double(x+70,y,x+35,y-20));   // x+ 70,y,x+35,y-10
		gg.draw( new Line2D.Double(x,y,x,y+40));		//x,y,x,y+40
		gg.draw( new Line2D.Double(x+70,y,x+70,y+40));	//x+70,y,x+70,y+40
		gg.draw( new Line2D.Double(x,y+40,x+35,y+60));
		gg.draw( new Line2D.Double(x+70,y+40,x+35,y+60));
		
		gg.draw( new Line2D.Double(x+35,y-15,x+65,y+2));		//x+35,y-15,x+65,y+2
		gg.draw( new Line2D.Double(x+5,y+2.5f,x+5,y+37.5f));	//x+5,y+2.5f,x+5,y+27.5f
		gg.draw( new Line2D.Double(x+35,y+55,x+65,y+38));		//x+35,y+55,x+65,y+38
		
		
		if(drawSavedItem == true)
		{
			for (int i = 0; i <ROWCOUNT; i++)
			for(int j = 0; j<9;j++)
				{
				gg.draw(benzeneArr[i][j]);
				}
			}
		}
		
		//Sum of angles = (sides - 2)(180)
		//Triangle = 180
		// Square = 360
		//Pentagon = 540 ....etc.
	
	
		
	
	void benzeneFill()
	{	
		benzeneArr[ROWCOUNT][0] = new Line2D.Double(x,y,x+35,y-20);
		benzeneArr[ROWCOUNT][1] = new Line2D.Double(x+70,y,x+35,y-20);
		benzeneArr[ROWCOUNT][2] = new Line2D.Double(x,y,x,y+40);
		benzeneArr[ROWCOUNT][3] =new Line2D.Double(x+70,y,x+70,y+40);
		benzeneArr[ROWCOUNT][4] = new Line2D.Double(x,y+40,x+35,y+60);
		benzeneArr[ROWCOUNT][5] = new Line2D.Double(x+70,y+40,x+35,y+60);
		benzeneArr[ROWCOUNT][6]= new Line2D.Double(x+35,y-15,x+65,y+2);
		benzeneArr[ROWCOUNT][7] = new Line2D.Double(x+5,y+2.5f,x+5,y+37.5f);
		benzeneArr[ROWCOUNT][8] = new Line2D.Double(x+35,y+55,x+65,y+38);
		
		ROWCOUNT++;
	}
	
	void clearJPanel()
	{
		drawSavedItem = false;
		for(int i = 0; i < 100; i++)
			for(int j =0; j < 9; j++)
			{
				benzeneArr[i][j] = new Line2D.Double(0,0,0,0);			    
			}
		drawSavedItem = true;
		repaint();
	}
	
	public void setBackgroundColor(Color color)
	{
		backgroundColor = color;
		repaint();
	}

	private class MouseMovingHandler extends MouseMotionAdapter
	{
	
		public void mouseMoved(MouseEvent e)
		{
			
			 x = (int)(e.getPoint().getX());
			 y = (int)(e.getPoint().getY());
	
				repaint();
			
		}
	}
	private class MouseHandler extends MouseAdapter
	{
		public void mousePressed(MouseEvent e)
		{
			if(e.getModifiers() == e.BUTTON1_MASK)
			{
				drawSavedItem = true;
				benzeneFill();
				repaint();
			
				//mainHandle.addMessage("You selected: " + selected);	//delete this
			}
		
			}
		}
	
	
}
